﻿

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using GPCSC.Web;
using GPCSC.BLL;
using NickLee.Web.UI;
using NickLee.Common;
using GPCSC.Model;


namespace GPCSC.Web.SYS_ORG
{
    public partial class ATIONTreeView : PageBase
    {
        SqlReadXml org = new SqlReadXml();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                f_SelectDataBind();
            }
        }


        /// <summary>
        /// other bind,UI Bind:其他数据邦定
        /// </summary>
        private void f_SelectDataBind()
        {
            DataSet set1 = new DataSet();
            TreeViewNode node = new TreeViewNode();
            baseTree.Nodes.Clear();
            node = CreateTreeNode("系统角色", "SystemRole", true, "SystemRole", "系统角色", "");
            node.Target = "TOPTREEVIEW";
            this.baseTree.Nodes.Add(node);

            set1 = org.ReaderDataSet("sel_rolelist_all2");//原来是sel_grouplist_all2，换为sel_rolelist_all2
            buildTree_JS(baseTree, set1);


            //}
            //baseTree.Nodes.Sort("Name", true);
        }

       


        /// <summary>
        /// 给所有node附加属性
        /// </summary>
        /// <param name="text">呈现文本</param>
        /// <param name="ID">主键</param>
        /// <param name="expand">是否扩展</param>
        /// <param name="Value">后台值</param>
        /// <param name="toolTip">鼠标移动上去的文本提示</param>
        /// <param name="imgUrl">对应的图片链接</param>
        /// <returns>返回node</returns>
        private TreeViewNode CreateTreeNode(string text, string ID, bool expand, string Value, string toolTip, string imgUrl)
        {
            TreeViewNode node = new TreeViewNode();
            node.Text = text;
            node.ID = ID;
            node.Expanded = expand;
            node.Value = Value;
            node.ToolTip = "点击" + text + ",查看" + text + "用户列表或者用户权限.";
            /*
             * 如果需要后台导航，请去掉该属性，并在改类中修改相应函数
             * node.NavigateUrl=url;
             * */
            node.ImageUrl = imgUrl;
            //node.Target = "POST";
            //node.Selectable = true;
            //node.ContentCallbackUrl = "ATIONTreeViewXml.aspx?ParentNodeId=" + ID.ToString();
            //node.ContentCallbackUrl ="";
            return node;
        }
    
  

        #region TreeView结构无线级填充 for 系统角色

        private bool buildTree_JS(NickLee.Web.UI.TreeView TreeViewName, DataSet ds)
        {
            baseTree.FindNodeById("SystemRole").Nodes.Clear();

            //如果为最高级，则修改以适应ds的relation中的关系
            foreach (DataRow dbRow in ds.Tables[0].Rows)
            {
                if (dbRow["CL_PARROLEID_PK"] != null && Usual.f_convertString(dbRow["CL_PARROLEID_PK"]) == "00000000-0000-0000-0000-000000000000")
                    //if (dbRow["PARMENUID"].ToString() == "0")
                    //{
                    dbRow["CL_PARROLEID_PK"] = DBNull.Value;
                //}
            }
            //设置ds中父子级relation
            try
            {
                if (ds.Relations["NodeRelation"] == null)
                {
                    ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["CL_ROLE_SOID"], ds.Tables[0].Columns["CL_PARROLEID_PK"]);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            //循环绑定父node
            foreach (DataRow dbRow in ds.Tables[0].Rows)
            {
                if (dbRow.IsNull("CL_PARROLEID_PK"))
                {
                    TreeViewNode newNode = CreateTreeNode_JS(dbRow["ROLENAME"].ToString(), dbRow["CL_ROLE_SOID"].ToString(), true, dbRow["CL_ROLE_SOID"].ToString(),
                        dbRow["ROLENAME"].ToString(), "contacts.gif");
                    baseTree.FindNodeById("SystemRole").Nodes.Add(newNode);
                    PopulateSubTree_JS(dbRow, newNode);
                }
            }
            return true;
        }


        /// <summary>
        /// 循环绑定子node
        /// </summary>
        /// <param name="dbRow">dataset的行</param>
        /// <param name="node">需要添加的节点</param>
        private void PopulateSubTree_JS(DataRow dbRow, TreeViewNode node)
        {
            foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation"))
            {
                TreeViewNode childNode =
                    CreateTreeNode_JS(childRow["ROLENAME"].ToString(), childRow["CL_ROLE_SOID"].ToString(), true, childRow["CL_ROLE_SOID"].ToString(),
                    childRow["ROLENAME"].ToString(), "contacts.gif");
                node.Nodes.Add(childNode);
                PopulateSubTree_JS(childRow, childNode);
            }
        }



        /// <summary>
        /// 给所有node附加属性
        /// </summary>
        /// <param name="text">呈现文本</param>
        /// <param name="ID">主键</param>
        /// <param name="expand">是否扩展</param>
        /// <param name="Value">后台值</param>
        /// <param name="toolTip">鼠标移动上去的文本提示</param>
        /// <param name="imgUrl">对应的图片链接</param>
        /// <returns>返回node</returns>
        private TreeViewNode CreateTreeNode_JS(string text, string ID, bool expand, string Value, string toolTip, string imgUrl)
        //string url
        {
            TreeViewNode node = new TreeViewNode();
            node.Text = text;
            node.ID = ID;
            node.Expanded = expand;
            node.Value = Value;
            node.ImageUrl = imgUrl;
            node.Target = "ROLE";
            node.Selectable = true;

            return node;
        }
        #endregion


        
    }
}
